package com.csu.service.serviceImpl;

import com.csu.entity.News;
import com.csu.mapper.NewsMapper;
import com.csu.service.RichTextService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.UUID;

@Service
public class RichTextServiceImpl implements RichTextService {
    @Value("${imageBasePath}")
    private String imageBasePath;
    @Value("${fileHTTPImageBasePath}")
    private String fileHTTPImageBasePath;
    @Value("${htmlBasePath}")
    private String htmlBasePath;
    @Value("${fileHTTPHtmlBasePath}")
    private String fileHTTPHtmlBasePath;
    @Value("${voiceBasePath}")
    private String voiceBasePath;
    @Value("${fileHTTPVoiceBasePath}")
    private String fileHTTPVoiceBasePath;
    @Autowired
    private NewsMapper newsMapper;
    @Override
    public String downloadFile(MultipartFile imageFile,int type) throws IOException {
        String fileBasePath = "";
        String fileHTTPBasePath = "";
        // 获取上传文件的原始文件后缀名
        String originalFilename = imageFile.getOriginalFilename();
        //上传图片
        if (type == 0){
            fileBasePath = imageBasePath;
            fileHTTPBasePath = fileHTTPImageBasePath;
        } else if (type == 2) {
            fileBasePath = voiceBasePath;
            fileHTTPBasePath = fileHTTPVoiceBasePath;
        } else{
            fileBasePath = htmlBasePath;
            fileHTTPBasePath = fileHTTPHtmlBasePath;
            originalFilename += ".html";
        }

        if (originalFilename == null){
            throw new NullPointerException();
        }

        // 生成一个唯一的随机文件名
        String newFilename = UUID.randomUUID().toString()+originalFilename;
        // 构建目标保存路径
        String targetPath = fileBasePath + newFilename;

        // 创建目标文件
        Path path = Paths.get(targetPath);

        // 保存文件
        Files.copy(imageFile.getInputStream(), path);

        //返回图片的请求路径
        return fileHTTPBasePath+newFilename;
    }

    @Override
    public String insertNews(News news) {
        int count = newsMapper.insertNews(news);
        if (count == 1)
            return "插入成功！";
        else
            return "插入失败！";
    }

    /**
     * 保存HTML文件
     * @param fileHtml
     * @return String
     */
    @Override
    public String saveHtmlFile(String fileHtml) {
        String fileBasePath = htmlBasePath;
        String fileHTTPBasePath = fileHTTPHtmlBasePath;
        // 生成一个唯一的随机文件名
        String newFilename = UUID.randomUUID().toString()+".html";
        // 构建目标保存路径
        String filePath = fileBasePath + newFilename;
        System.out.println("filePath:"+filePath);
        try (BufferedWriter writer = new BufferedWriter(new FileWriter(filePath))) {
            writer.write(fileHtml);
            System.out.println("HTML文件创建成功！");
        } catch (IOException e) {
            System.out.println("创建HTML文件失败：" + e.getMessage());
        }
        return fileHTTPBasePath+newFilename;
    }
}
